﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace eDoc.Controls.Blacklist
{
    public partial class ucList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            DataTable dt = new DataTable();
            dt = BO.Data.Select_Blacklist();
            grdBlacklist.DataSource = dt;
            grdBlacklist.DataBind();
        }

        private void ExportToPDF()
        {
            string fileName = "Blacklist_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".pdf";
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            grdBlacklist.AllowPaging = false;
            grdBlacklist.HeaderRow.Style.Add("font-size", "13px");
            grdBlacklist.HeaderRow.Style.Add("background-color", "#cdcdcd");
            grdBlacklist.HeaderRow.Style.Add("color", "blue");
            grdBlacklist.HeaderRow.Style.Add("text-align", "center");
            grdBlacklist.HeaderRow.Style.Add("height", "30px");
            grdBlacklist.Style.Add("text-decoration", "none");
            grdBlacklist.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdBlacklist.Style.Add("font-size", "12px");
            grdBlacklist.RowStyle.Height = 25;
            divBlacklistText.InnerHtml = "Danh sach thiet bi trong Blacklist";
            divBlacklistText.Style.Add("text-align", "center");
            divBlacklistText.Style.Add("padding-bottom", "30px");
            divBlacklistText.Style.Add("font-size", "20px");
            divBlacklist.RenderControl(htmlTextWriter);
            StringReader stringReader = new StringReader(stringWriter.ToString());

            /*
            using (Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f))
            {
                PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
                doc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(
                  writer, doc, stringReader
                );
            }
            */
            Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(Doc, Response.OutputStream);

            Doc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(
                  writer, Doc, stringReader
                );
            Doc.Close();
            Response.Write(Doc);
            Response.End();

        }

        protected void btExportPDF_Click(object sender, EventArgs e)
        {
            ExportToPDF();
        }
    }
}